import java.io.*;
import java.net.*;

class UDPClient {

    int portnumber;
    String hostname;

    UDPClient(int portnumber, String hostname) {
        this.portnumber = portnumber;
        this.hostname = hostname;
    }

    public void run() throws Exception {
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        DatagramSocket clientSocket = new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByName(hostname);


        byte[] sendData = new byte[1024];
        byte[] receiveData = new byte[1024];
        System.out.println("Running UDP client, enter command");
        String sentence = inFromUser.readLine();                    // lesum það sem notandi skráði inn
        sendData = sentence.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, portnumber);
        clientSocket.send(sendPacket);                              // sendum pakkann til serversins
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);                        // tökum á móti pakka
        String modifiedSentence = new String(receivePacket.getData());
        System.out.println("FROM SERVER:" + modifiedSentence);     // birtum niðurstöðu
        clientSocket.close();
    }
}

